<template>
    <div>
        用户名：<input type="text" placeholder="用户名" v-model="info.username"><br>
        手机号：<input type="text" placeholder="手机号" v-model="info.phone"><br>
        城市<input type="text" placeholder="城市" v-model="info.city"><br>
        详细地址：<input type="text" placeholder="详细地址：" v-model="info.address"><br>
        <button @click="add">{{info.id?'修改':'添加'}}</button>
    </div>
</template>

<script>
import request from '../util/request'
export default {
    created(){
        if(localStorage.getItem('info')){
            let {username,phone,id,city,address} = JSON.parse(localStorage.getItem('info'));
            this.info = {
                username,
                phone,
                city,
                address,
                id
            }
        }
    },
    data(){
        return {
            info:{
                username:'',
                phone:'',
                city:'',
                address:'',
                id:''
            }
        }
    },
    methods:{
        add(){
            let url = '';
            if(this.info.id){
                //修改接口
                url = '/api/update';
            }else{
                //添加接口
                url = '/api/add';
            }
            //添加接口  修改接口
            request.post(url,{...this.info}).then(res => {
                if(res.code === 1){
                    console.log("添加成功");
                    alert("成功");
                    localStorage.removeItem('info');
                    this.$router.push('/list');
                }else{
                    console.log("失败")
                }
            })
        }
    }
}
</script>